package controller;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextPane;

import model.Transaction;
import daoImpl.TransactionDaoImpl;


public class GUI implements Observer{
	
	private JFrame		frame;
	private JTextPane	textPane;
	private Observable	observalble;
	private JPanel		listPanel;
	private JList		vegs;
	
	
	/**
	 * Launch the application.
	 */
	public static void start( final Observable observer ) {
		EventQueue.invokeLater( new Runnable(){
			
			public void run() {
				try{
					GUI window = new GUI( observer );
					window.frame.setVisible( true );
				} catch( Exception e ){
					e.printStackTrace();
				}
			}
		} );
	}
	
	
	/**
	 * Create the application.
	 */
	public GUI( Observable observalbe ) {
		this.observalble = observalbe;
		observalble.addObserver( this );
		init( new ArrayList<Transaction>() );
	}
	
	
	private void init( List<Transaction> transactions ) {
		frame = new JFrame();
		
		// make sure the program exits when the frame closes
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		frame.setTitle( "Example GUI" );
		frame.setSize( 300, 250 );
		
		// This will center the JFrame in the middle of the screen
		frame.setLocationRelativeTo( null );
		
		// Options for the JList
		String[] vegOptions = { "test" };
		if( !transactions.isEmpty() ){
			ArrayList<String> list = new ArrayList<String>();
			for( Transaction tran : transactions ){
				list.add( tran.getDescription() );
			}
			vegOptions = ( String[] ) list.toArray();
		}
		
		// The first JPanel contains a JLabel and JCombobox
		
		// Create the second JPanel. Add a JLabel and JList and
		// make use the JPanel is not visible.
		listPanel = new JPanel();
		listPanel.setVisible( true );
		JLabel listLbl = new JLabel( "Vegetables:" );
		vegs = new JList( vegOptions );
		vegs.setLayoutOrientation( JList.HORIZONTAL_WRAP );
		
		listPanel.add( listLbl );
		listPanel.add( vegs );
		
		// The JFrame uses the BorderLayout layout manager.
		// Put the two JPanels and JButton in different areas.
		// frame.add(comboPanel, BorderLayout.NORTH);
		frame.add( listPanel, BorderLayout.CENTER );
		
		// make sure the JFrame is visible
		frame.setVisible( true );
	}
	
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds( 100, 100, 450, 300 );
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		textPane = new JTextPane();
		textPane.add( new JLabel( "TEST" ) );
		textPane.add( new JLabel( "TESTWEAL;GFWEAJEW" ) );
		frame.add( textPane );
	}
	
	
	@Override
	public void update( Observable obs, Object arg1 ) {
		if( obs instanceof TransactionDaoImpl ){
			TransactionDaoImpl dao = ( TransactionDaoImpl ) obs;
			init( dao.transactions );
			//
			
		}
		// TODO Auto-generated method stub
		
	}
	
}
